package com.bootdo.lvyou.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.haojiangboutils.MapBuild;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouScenicTicketMoney;
import com.bootdo.lvyou.service.LvyouScenicTicketMoneyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 门票价格
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-11 20:38:00
 */
 
@Controller
@RequestMapping("/lvyou/lvyouScenicTicketMoney")
public class LvyouScenicTicketMoneyController extends BaseController{


	@Autowired
	private LvyouScenicTicketMoneyService lvyouScenicTicketMoneyService;
	
	@ModelAttribute
	public LvyouScenicTicketMoney get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouScenicTicketMoneyService.get(id);
		}else{
			return new LvyouScenicTicketMoney();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouScenicTicketMoney','lvyou:lvyouScenicTicketMoney:lvyouScenicTicketMoney',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouScenicTicketMoney:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:lvyouScenicTicketMoney")	
	String LvyouScenicTicketMoney(LvyouScenicTicketMoney lvyouScenicTicketMoney,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicTicketMoney/lvyouScenicTicketMoney";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:lvyouScenicTicketMoney")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouScenicTicketMoney> lvyouScenicTicketMoneyList = lvyouScenicTicketMoneyService.list(query);
		int total = lvyouScenicTicketMoneyService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouScenicTicketMoneyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:edit")
	String add(LvyouScenicTicketMoney lvyouScenicTicketMoney,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicTicketMoney/lvyouScenicTicketMoneyedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouScenicTicketMoney lvyouScenicTicketMoney) {
		return R.ok().put("i",lvyouScenicTicketMoney);
	}

	/**
	 * /lvyou/lvyouScenicTicketMoney/getByNowDate
	 * @return
	 */
	@PostMapping("/getByNowDate")
	@ResponseBody
	R getByNowDate(@RequestParam("ticketId") Long ticketId
			  ) {
		Map<String,Double> resultMap = new HashMap();
		List<LvyouScenicTicketMoney> list =
				lvyouScenicTicketMoneyService.list(MapBuild.getInstand().setKV("ticketId",ticketId)
				.setKV("nowDateFm",DateUtils.format(new Date(), DateUtils.DATE_PATTERN)));
		list.forEach(item ->{
			resultMap.put(DateUtils.format(item.getNowDate(),
					DateUtils.DATE_PATTERN),item.getMoney());
		});
		return R.ok().put("i",resultMap);
	}


	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:edit")
	public R save( LvyouScenicTicketMoney lvyouScenicTicketMoney){
		if(lvyouScenicTicketMoney.getMoney() == null || lvyouScenicTicketMoney.getMoney() < 0){
			return R.error("请输入正确的价格");
		}
		LvyouScenicTicketMoney lvyouScenicTicketMoney2 =  lvyouScenicTicketMoneyService.
				getByNowDate(lvyouScenicTicketMoney.getTicketId(),
						DateUtils.format(lvyouScenicTicketMoney.getNowDate(),
								DateUtils.DATE_PATTERN));
		if(null != lvyouScenicTicketMoney2){
			Double money = lvyouScenicTicketMoney.getMoney();
			lvyouScenicTicketMoney = lvyouScenicTicketMoney2;
			lvyouScenicTicketMoney.setMoney(money);
		}
		if(lvyouScenicTicketMoneyService.save(lvyouScenicTicketMoney)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:edit")
	public R remove( Long id){
		if(lvyouScenicTicketMoneyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicTicketMoney:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouScenicTicketMoneyService.batchRemove(ids);
		return R.ok();
	}
	
}